﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmitemtypelist
    Dim Strsql As String
    Dim _Mysql As New ClsSQLhelper
    Dim On_load As Boolean = True
    Public Primary_Key As Integer = -1
    Dim DTSection As New DataTable
#Region "Function"
    Sub Load_Data()
        Try
            Strsql = "select itemnumber,itemtype.itemname,alternatenumber,uom,registerdate,itemtype.active,itemtype.note,section_name,Category_name,cost,standdardprice  from itemtype  "
            Strsql = Strsql & " left join section on itemtype.section= section.section_id "
            Strsql = Strsql & " left join category on  itemtype.category = category.Category_id "

            Strsql = Strsql & " where section = " & Me.Cbosection.SelectedValue & " and category = " & CboCategory.SelectedValue

            Strsql = Strsql & " order by section.section_name,category.Category_name,itemname"


            Dim DT_Items As New DataTable
            DT_Items = _Mysql.GetMYSQLDataTable(Strsql, "itemtype")
            With GridControl1

                .DataSource = DT_Items

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelete_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub
    Sub Load_ProductGroup()
        Try
            Strsql = "Select section_id,section_name from section order by readonly desc,section_name"

            DTSection = _Mysql.GetMYSQLDataTable(Strsql, "section")
            With Me.Cbosection
                .DataSource = DTSection
                .DisplayMember = "section_name"
                .ValueMember = "section_id"
            End With
            Cbosection.SelectedValue = My.Settings.Current_Item_Section
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
     

    End Sub
    Sub Load_ProductType(Optional ByVal ProductGroup As Integer = 0)
        Try
            Strsql = "Select Category_id,Category_name from category where sectionid = " & ProductGroup & " order by readonly desc,Category_name"
            Dim DTcategory As New DataTable
            DTcategory = _Mysql.GetMYSQLDataTable(Strsql, "category")
            With Me.CboCategory
                .DataSource = DTcategory
                .DisplayMember = "Category_name"
                .ValueMember = "Category_id"
            End With
            CboCategory.SelectedValue = My.Settings.Current_Item_Category
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
       
    End Sub
#End Region
    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub
    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try
            Dim Gv As GridView = sender
            Primary_Key = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("itemnumber"))

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub frmonhand_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Load_ProductGroup()
        Load_ProductType(Cbosection.SelectedValue)
        Load_Data()


        On_load = False
    End Sub

    Private Sub Cbosection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cbosection.SelectedIndexChanged


        If On_load = True Then Exit Sub
        My.Settings.Current_Item_Section = Cbosection.SelectedValue
        Load_ProductType(Me.Cbosection.SelectedValue)
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        frmItemType.NewEntry_State = True
        If frmItemType.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
        Load_Data()
    End Sub


    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        If Primary_Key = -1 Then Exit Sub
        frmItemType.NewEntry_State = False
        frmItemType.Primary_Key = Primary_Key
        If frmItemType.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
        Load_Data()
    End Sub

    Private Sub BtnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefresh.Click
        Load_Data()

    End Sub

    Private Sub CboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategory.SelectedIndexChanged

        If On_load = True Then Exit Sub
        My.Settings.Current_Item_Category = CboCategory.SelectedValue
        Load_Data()

    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If MsgBox("ต้องการลบสินค้าชนิดนี้ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบสินค้า") = MsgBoxResult.No Then Exit Sub


        Try
            Strsql = "Select count(*) as cc from pr_detail where item_type_id = " & Primary_Key
            If _Mysql.MySQLExecuteScalar(Strsql) > 0 Then
                MsgBox("ขออภัยไม่สามารถลบได้เนื่องจาก รหัสนี้ได้มีการใช้งานไปแล้ว", MsgBoxStyle.Exclamation, "กรุณาติดต่อผู้ดูแลระบบ")
                Exit Sub
            End If
        Catch ex As Exception

        End Try
        Try
            Strsql = "delete from itemtype where itemnumber = " & Primary_Key
            If _Mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDelete_Click : delete detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
                Primary_Key = -1
                Load_Data()

            End If
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelete_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
End Class